package cn.com.huzo.service.inventory.impl;

import cn.com.huzo.model.entities.inventory.StockOutHeaderEntity;
import huzofw.model.entities.paginator.Paginator;
import cn.com.huzo.repository.inventory.StockOutHeaderRepository;
import cn.com.huzo.repository.inventory.StockOutLinesRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.inventory.StockOutHeaderManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 领料出库单管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("StockOutHeaderManager")
public class StockOutHeaderManagerImpl extends GenericManagerImpl<StockOutHeaderEntity, Long>
        implements StockOutHeaderManager {
    
    @Resource(name = "StockOutHeaderRepository")
    private StockOutHeaderRepository _dao_header;
    
    @Resource(name = "StockOutLinesRepository")
    private StockOutLinesRepository _dao_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @Transactional
    public Map<String, Object> fetchByHeaderKey(Long headerId, Long currentUserId, Long workFlowId) throws Exception {
        Map<String, Object> _result = new HashMap();

        // 得到领料出库单表头信息
        _result.put("HeaderEntity", _dao_header.fetchByKey(headerId, currentUserId, workFlowId));
        // 得到明细信息
        _result.put("LinesList", _dao_lines.fetchByHeaderId(headerId));
        
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        
        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao_header.fetchForCount(criterion);
        
        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<StockOutHeaderEntity> itemList = _dao_header.fetchForPage(criterion);
            Paginator<StockOutHeaderEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);
            
            _result.put("TotalRecordCount", paginator.getTotal());
            _result.put("TotalPages", paginator.getTotalPages());
            _result.put("PageIndex", paginator.getPageIndex());
            _result.put("DataList", paginator.getItemList());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }
        
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> save(StockOutHeaderEntity instance) throws Exception {
        Map<String, Object> _result = new HashMap();
        Long key = _dao_header.insert(instance);
        if (key != null && key > 0) {
            _result.put("Key", key);
            _dao_lines.insert(key, instance.getRsHeaderId(), instance.getUpdatedBy());
            _result.put("LinesList", _dao_lines.fetchByHeaderId(key));
            // 审计日志
            writeAuditLog(instance.getUpdatedBy(), "库存管理→领料出库单", "领料出库单据号：" + instance.getBillNo(), "增加");
        }
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer update(StockOutHeaderEntity instance) throws Exception {
        Integer rowsAffected = _dao_header.update(instance);
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(instance.getUpdatedBy(), "库存管理→领料出库单", "领料出库单据号：" + instance.getBillNo(), "更新");
        }
        return rowsAffected;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Integer rowsAffected = _dao_header.delete(MapUtils.getLong(criterion, "HeaderId"));
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
                    "库存管理→领料出库单", "领料出库单据号：" + MapUtils.getString(criterion, "DeletedBillNo"),
                    "删除");
        }
        return fetchForPage(criterion);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public String fetchNewBillNo() throws Exception {
        return _dao_header.fetchNewBillNo();
    }
    
}
